#include "../includes.h"


CONFIG_     CONFIG;

void Show_Config(void);
void Setting_CONFIG(void);


//typedef struct CF_{
//	int id;			//ID
//	double v;		//Value
//	char type;		//Type
//	char *name;	//Name
//	double min;		//Min
//	double max;		//Max
//	double dft;		//Default
//}ConfigFormat;

//enum ConfigDefine{ cTimeMode = 0, 
//					cLOCALHOUR,
//					cLOCALMINUTE, 
//					cANTDLY,
//					cAntCheckFlag,
//					cPositionMode,
//					cLAT,
//					cLON,
//					cHEI,
//					cDAC,
//					cGAIN,
//					cMASK,
//					cOCXO_PATH,
//					cOCXO_ID,
//					cOCXO_CID,
//					cOCXO_TID,
//					cAlarmRange,
//					cHoldoverDuration,
//					cWarmStartLimit,
//					cbeep_flag,
//					copenshort_flag,
//					cdelay_value,
//					cTEMPER_CONTROL,
//					cWriteEvent,
//					cHWrevision,
//					cEnd
//				};


//#define CONFIG_INDEX	50
ConfigFormat cfConfig[ CONFIG_INDEX ]={
//       ID                 Value       Type        Name                   Min             Max            Default
	{ cTimeMode, 			0,			'c',		"TimeMode",				0,				2,				0 },
	{ cLOCALHOUR, 			0,			'i',		"LocalHour",			0,				23,				0 },
	{ cLOCALMINUTE, 		0,			'i',		"LocalMinute",			0,				59,				0 },
	{ cANTDLY, 				120,		'i',		"ANTDLY",				0,				1000000,		120 },
	{ cAntCheckFlag, 		1,			'c',		"AntCheckFlag",			0,				1,				1 },
	{ cPositionMode, 		0,			'c',		"PositionMode",			0,				2,				0 },
	{ cLAT, 				0,			'i',		"LAT",					0,				324000000,		0 },
	{ cLON, 				0,			'i',		"LON",					0,				648000000,		0 },
	{ cHEI, 				0,			'i',		"HEI",					0,				100000,			0 },
	{ cDAC, 				25000,		'i',		"DAC",					0,				65000,			32000 },
//	{ cGAIN, 				1.24E-11,	'd',		"GAIN",					1.24E-12,		5E-10,		1.24E-11 },
//	{ cGAIN, 			5.12E-13,		'd',		"GAIN",					5.2E-13,		5.2E-10,		5.096435E-13 },
	{ cGAIN, 			5.12E-13,		'd',		"GAIN",					5.2E-13,		5.2E-10,		5.12E-13 },
	{ cMASK, 				10,			'i',		"MASK",					0,				20,				10 },
	{ cOCXO_PATH, 			0,			'c',		"/gpsclock/",			0,				1,				0 },
	{ cOCXO_ID, 			34833,		'c',		"OCXOID",				0,				60000,			34833 },
	{ cOCXO_CID, 			0,			'c',		"_cid",					0,				1,				0 },
	{ cOCXO_TID, 			0,			'c',		"_tid",					0,				1,				0 },
	{ cAlarmRange, 			100,		'i',		"AlarmRange",			1,				1000,			100 },
	{ cHoldoverDuration, 	10,			'i',		"HoldoverDuration",		0,				20,				10 },
	{ cWarmStartLimit, 		120,		'i',		"WarmStartLimit",		100,			140,			120 },
	{ cbeep_flag, 			1,			'c',		"BeepFlag",				0,				1,				1 },
	{ copenshort_flag, 		1,			'c',		"OpenShortflag",		0,				1,				1 },
	{ cdelay_value, 		13,			'i',		"DelayValue",			0,				20,				13 },
	{ cTEMPER_CONTROL,		1,			'c',		"TemperControl",		0,				1,				1 },
	{ cWriteEvent, 			0,			'c',		"WriteEvent",			0,				1,				1 },
	{ cHWrevision,			1,			'i',		"HWrevision",			1,				100,			2 },
	{ cGPSRceiverType,		0,			'i',		"ReceiverType",			0,				2,				0 },
	{ cStatusReportPeriod,	30,			'i',		"StatusReportPeriod",	10,				960,			30 },
	{ cHeartBeatPeriod,		0,			'i',		"HeartBeatPeriod",		0,				30,				0 },
	{ cTemperatureGain,		200,		'i',		"TemperatureGain",		-3000,			3000,			0 }, // unit 1e-12
	{ cCVType,		0,			'i',		"CVType",			0,				1,				0 },
	{ cPosX, 				0,			'd',		"PosX",					-324000000,	324000000,		0 },
	{ cPosY, 				0,			'd',		"PosY",					-324000000,	324000000,		0 },
	{ cPosZ, 				0,			'd',		"PosZ",					-324000000,	324000000,		0 },
	{ cSYSTEM_DELAY, 		0,			'd',		"SYSTEM_DELAY",		-324000000,	324000000,		0 },
	{ cFPGACH, 				0,			'i',		"FPGACH",				0,				1,					0 },
	{ cVMEMODE,				0,			'i',		"VMEMODE",				0,				1,					0 },
	{ cNETSEND,				0,			'i',		"NETSEND",				0,				1,					0 },

	{ cEnd, 				0,			'c',		"End",					0,				1,				0 }
};


#define		_U		0x01	/* upper */
#define		_L		0x02	/* lower */
#define		_D		0x04	/* digit */
#define		_C		0x08	/* cntrl */
#define		_P		0x10	/* punct */
#define		_S		0x20	/* white space (space/lf/tab) */
#define		_X		0x40	/* hex digit */
#define		_SP	0x80	/* hard space (0x20) */

#define		__ismask(x)	(_ctype[(int)(unsigned char)(x)])
//#define		islower(c)		((__ismask(c)&(_L)) != 0)
//#define		isupper(c)		((__ismask(c)&(_U)) != 0)

unsigned char _ctype[] = {
_C,_C,_C,_C,_C,_C,_C,_C,								/* 0-7 */
_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,					/* 8-15 */
_C,_C,_C,_C,_C,_C,_C,_C,								/* 16-23 */
_C,_C,_C,_C,_C,_C,_C,_C,								/* 24-31 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P,							/* 32-39 */
_P,_P,_P,_P,_P,_P,_P,_P,								/* 40-47 */
_D,_D,_D,_D,_D,_D,_D,_D,								/* 48-55 */
_D,_D,_P,_P,_P,_P,_P,_P,								/* 56-63 */
_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,				/* 64-71 */
_U,_U,_U,_U,_U,_U,_U,_U,								/* 72-79 */
_U,_U,_U,_U,_U,_U,_U,_U,								/* 80-87 */
_U,_U,_U,_P,_P,_P,_P,_P,								/* 88-95 */
_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,				/* 96-103 */
_L,_L,_L,_L,_L,_L,_L,_L,								/* 104-111 */
_L,_L,_L,_L,_L,_L,_L,_L,								/* 112-119 */
_L,_L,_L,_P,_P,_P,_P,_C,								/* 120-127 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,							/* 128-143 */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,							/* 144-159 */
_S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,		/* 160-175 */
_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,			/* 176-191 */
_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,				/* 192-207 */
_U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L,				/* 208-223 */
_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,				/* 224-239 */
_L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L};				/* 240-255 */


//double my_atof (const char *string)
//{
//    double result;
//
//    /* this still does not detect '12ab' as invalid */
//    if (sscanf(string, "%lf", &result) != 1) return 0;
//
//    return result;
//}
#define is_num(c)	((c) >='0' && (c)<='9')
int is_float(char *s)
{
	int bPoint = 0;
	int bExponet = 0;
	char *str=s;
	int cnt=0;
	while( *s )
	{
		if(*s == '\n' || *s == '\r')
		{
			*s=0;
			//printf("\nCRLF:%d\n",cnt);
			if(cnt) return 1;
			else return 0;
		}
		if( *s=='-' ||*s=='+'  ){
			if( str!=s && (*(s-1) =='e' || *(s-1) =='E') ) ;
			else if(s!=str ){
				//printf("f:00");
				return 0;
			}
		}
		else
		if( *s == '.' )
		{
			if(bPoint || bExponet){
				//printf("f:01");
				return 0;
			}
			else bPoint = 1;
		}
		else if( *s == 'E' || *s == 'e')
		{
			if(bExponet)
			{
				//printf("f:02");
				return 0;
			}
			else bExponet = 1;
		}
		else if(!is_num(*s))
		{
			//printf("f:03:%c,%d",*s,cnt);
			return 0;
		}
		s++;
		cnt++;
	}
	return 1;
}

int is_int(char *s)
{
	int bPoint = 0;
	int bExponet = 0;
	char *str=s;
	int cnt=0;
	while( *s )
	{
		if(*s == '\n' || *s == '\r')
		{
			*s=0;
			//printf("\nCRLF:%d\n",cnt);
			if(cnt) return 1;
			else return 0;
		}
		if( *s=='-' ||*s=='+'  ){ if(s!=str) return 0; }
		else
		if(!is_num(*s))return 0;
		s++;
		cnt++;
	}
	return 1;
}

double my_atof(char *s)
{
	if(!s) printf("--f:reject--\n");
	if( !is_float(s) )
		{
			if(s)
			{
				if(iLsiDebugVariable == 1) printf("---***float error:%s***",s);
			}
			else printf("\r\n**f:null**\r\n");
			return 0;
		}
	//else printf(" .");
	return atof(s);
}

long my_atol(char *s)
{
	if(!s) printf("--l:reject--\n");
	if( !is_int(s) )
		{
			if(s){ if(iLsiDebugVariable == 1) printf("***long error:%s***",s); }
			else printf("\r\n**l:null**\r\n");
			return 0;
		}
	//else printf(" .");
	return atol(s);
}


long my_atoi(char *s)
{
	if(!s) printf("--i:reject--\n");
	if( !is_int(s) )
	{
		if(s)
		{
			if(iLsiDebugVariable == 1){printf("---***int error:%s***",s);}
		}
		else printf("\r\n**i:null**\r\n");
		return 0;

	}
	//else printf(" .");
	return atoi(s);
}

void upper_str(char *Str)
{
	while( *Str )
	{
		if(islower(*Str))
		{
			*Str -= 'a'-'A';
		}
   		Str++; 
   }
} 

void lower_str(char *Str)
{
	while( *Str )
	{
		if(isupper(*Str))
		{
			*Str += 'a'-'A';
		}
   		Str++; 
   }
} 

#define MAX_ARGVS 16
char *delim = "=";
int parse_args(char *cmdline, char **argv)
{
	char *tok=NULL;
	int argc = 0;

	argv[argc] = NULL;
                   
	for(tok = strtok(cmdline, delim); ((tok) && (argc<MAX_ARGVS)); tok = strtok(NULL, delim)){
		argv[argc++] = tok;
	}
	return argc;
}



void Init_CONFIG()
{
	CONFIG.WriteEvent = OFF;
}


void RereadCONFIG()
{
	cfConfig[cTimeMode].v = CONFIG.TimeMode;
	
	cfConfig[cLOCALHOUR].v = CONFIG.LOCALHOUR;
    cfConfig[cLOCALMINUTE].v = CONFIG.LOCALMINUTE;
	
	cfConfig[cANTDLY].v = CONFIG.ANTDLY;
	cfConfig[cAntCheckFlag].v = CONFIG.AntCheckFlag;
	
	cfConfig[cPositionMode].v = CONFIG.PositionMode;
	
    cfConfig[cLAT].v = CONFIG.POSITION.LAT;
    cfConfig[cLON].v = CONFIG.POSITION.LON;
    cfConfig[cHEI].v = CONFIG.POSITION.HEI;

	cfConfig[cDAC].v = CONFIG.DAC;
	cfConfig[cGAIN].v = CONFIG.GAIN;
	
	cfConfig[cMASK].v = CONFIG.MASK;
	
	//-------------------------------------
	cfConfig[cOCXO_ID].v = atoi(CONFIG.OCXO_ID);
	//-------------------------------------
	
	cfConfig[cAlarmRange].v = CONFIG.AlarmRange;
	cfConfig[cHoldoverDuration].v = CONFIG.HoldoverDuration;
	
	cfConfig[cWarmStartLimit].v = CONFIG.WarmStartLimit;

	cfConfig[cbeep_flag].v = CONFIG.beep_flag; 

	cfConfig[copenshort_flag].v = CONFIG.openshort_flag;

	cfConfig[cdelay_value].v = CONFIG.delay_value;
	
	cfConfig[cTEMPER_CONTROL].v = CONFIG.TEMPER_CONTROL;
	
	cfConfig[cWriteEvent].v = CONFIG.WriteEvent;
	
	cfConfig[cHWrevision].v = CONFIG.HWrevision;

	cfConfig[cGPSRceiverType].v = CONFIG.RcvType;

	cfConfig[cStatusReportPeriod].v = CONFIG.iPeriodUdpReport;
	cfConfig[cHeartBeatPeriod].v = CONFIG.iPeriodHeartBeat;
	cfConfig[cTemperatureGain].v = CONFIG.iTemperatureGain;
	cfConfig[cCVType].v = CONFIG.iCVType;
	cfConfig[cPosX].v = CONFIG.PosX;
	cfConfig[cPosY].v = CONFIG.PosY;
	cfConfig[cPosZ].v = CONFIG.PosZ;

	cfConfig[cSYSTEM_DELAY].v = CONFIG.SYSTEM_DELAY;
	cfConfig[cFPGACH].v = CONFIG.FPGACH;
	cfConfig[cVMEMODE].v = CONFIG.VMEMODE;
	cfConfig[cNETSEND].v = CONFIG.NETSEND;



}

void PresetCONFIG()
{
	CONFIG.TimeMode = (char)cfConfig[cTimeMode].dft;
	
	CONFIG.LOCALHOUR = (char)cfConfig[cLOCALHOUR].dft;
    CONFIG.LOCALMINUTE = (char)cfConfig[cLOCALMINUTE].dft;
	
	CONFIG.ANTDLY = (long)cfConfig[cANTDLY].dft;
	CONFIG.AntCheckFlag = (char)cfConfig[cAntCheckFlag].dft;
	
	CONFIG.PositionMode = (char)cfConfig[cPositionMode].dft;
	
    CONFIG.POSITION.LAT = (long int)cfConfig[cLAT].dft;
    CONFIG.POSITION.LON = (long int)cfConfig[cLON].dft;
    CONFIG.POSITION.HEI = (long int)cfConfig[cHEI].dft;

	CONFIG.DAC = (int)cfConfig[cDAC].dft;
	CONFIG.GAIN = (float)cfConfig[cGAIN].dft;
	
	CONFIG.MASK = (char)cfConfig[cMASK].dft;
	
	//---------------------------------------------------------------
	//itoa((int)cfConfig[cOCXO_ID].dft, CONFIG.OCXO_ID, 10);
	sprintf(CONFIG.OCXO_ID, "%d", (int)cfConfig[cOCXO_ID].dft);
	//---------------------------------------------------------------
	
	CONFIG.AlarmRange = (unsigned short)cfConfig[cAlarmRange].dft;
	CONFIG.HoldoverDuration = (unsigned short)cfConfig[cHoldoverDuration].dft;
	
	CONFIG.WarmStartLimit = (int)cfConfig[cWarmStartLimit].dft;

	CONFIG.beep_flag = (char)cfConfig[cbeep_flag].dft; 

	CONFIG.openshort_flag = (char)cfConfig[copenshort_flag].dft;

	CONFIG.delay_value = (unsigned char)cfConfig[cdelay_value].dft;
	
	CONFIG.TEMPER_CONTROL = (char)cfConfig[cTEMPER_CONTROL].dft;
	
	CONFIG.WriteEvent = (char)cfConfig[cWriteEvent].dft;
	
	CONFIG.HWrevision = (int)cfConfig[cHWrevision].dft;
	
	CONFIG.RcvType = cfConfig[cGPSRceiverType].dft;


	CONFIG.iPeriodUdpReport = cfConfig[cStatusReportPeriod].dft;
	CONFIG.iPeriodHeartBeat = cfConfig[cHeartBeatPeriod].dft;

	CONFIG.iTemperatureGain = cfConfig[cTemperatureGain].dft;
	CONFIG.iCVType = cfConfig[cCVType].dft;
	CONFIG.PosX = 0;
	CONFIG.PosY = 0;
	CONFIG.PosZ = 0;

	CONFIG.SYSTEM_DELAY = 0;
	CONFIG.FPGACH = 0;
	CONFIG.VMEMODE = 1;
	CONFIG.NETSEND = 0;


	RereadCONFIG();
	
	LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, "Set to default");
	
    //kang LogIt(LOG_Set_To_Default,0);
}



void WriteCONFIG()
{
	FILE *fp;
	int i;

	CONFIG.WriteEvent = OFF;
	
	//fp = fopen("/gpsclock/config", "w+");
	fp = fopen("/app/config", "w+");
	
	if( fp == NULL ){
		printf( "Open Error... /app/config... for write+\n");
		LogItString (LOG_CATA1_NORM, LOG_CATA2_FAIL, "File open for write");
		exit(-1);
	}
	
	RereadCONFIG();
	
	for(i=0; i<cEnd; i++){
		if( (cfConfig[i].type == 'c') || (cfConfig[i].type == 'i') ){
			fprintf(fp, "%s=%d\n", cfConfig[i].name, (int)cfConfig[i].v );
		}
		else if(cfConfig[i].type == 'd'){
			//fprintf(fp, "%s=%.15f\n", cfConfig[i].name, cfConfig[i].v );
			fprintf(fp, "%s=%.2fE-11\n", cfConfig[i].name, cfConfig[i].v*1E11 );
		}
		else fprintf(fp, "%s=0\n", cfConfig[i].name);
	}
	
	fclose(fp);
	
	//LogItString (LOG_CATA1_NORM, LOG_CATA2_NOTHING, "Config updated");
}



void ApplyCONFIG()
{
	CONFIG.TimeMode = (char)cfConfig[cTimeMode].v;
	
	CONFIG.LOCALHOUR = (char)cfConfig[cLOCALHOUR].v;
    CONFIG.LOCALMINUTE = (char)cfConfig[cLOCALMINUTE].v;
	
	CONFIG.ANTDLY = (long)cfConfig[cANTDLY].v;
	CONFIG.AntCheckFlag = (char)cfConfig[cAntCheckFlag].v;
	
	CONFIG.PositionMode = (char)cfConfig[cPositionMode].v;
	
    CONFIG.POSITION.LAT = (long int)cfConfig[cLAT].v;
    CONFIG.POSITION.LON = (long int)cfConfig[cLON].v;
    CONFIG.POSITION.HEI = (long int)cfConfig[cHEI].v;

	CONFIG.DAC = (int)cfConfig[cDAC].v;
	CONFIG.GAIN = (float)cfConfig[cGAIN].v;
	
	CONFIG.MASK = (char)cfConfig[cMASK].v;
	
	//---------------------------------------------------------------
	sprintf(CONFIG.OCXO_ID, "%d", (int)cfConfig[cOCXO_ID].v);
	//---------------------------------------------------------------
	
	CONFIG.AlarmRange = (unsigned short)cfConfig[cAlarmRange].v;
	CONFIG.HoldoverDuration = (unsigned short)cfConfig[cHoldoverDuration].v;
	
	CONFIG.WarmStartLimit = (int)cfConfig[cWarmStartLimit].v;

	CONFIG.beep_flag = (char)cfConfig[cbeep_flag].v; 

	CONFIG.openshort_flag = (char)cfConfig[copenshort_flag].v;

	CONFIG.delay_value = (unsigned char)cfConfig[cdelay_value].v;
	
	CONFIG.TEMPER_CONTROL = (char)cfConfig[cTEMPER_CONTROL].v;
	
	CONFIG.WriteEvent = (char)cfConfig[cWriteEvent].v;
	
	CONFIG.HWrevision = (int)cfConfig[cHWrevision].v;

	CONFIG.RcvType = (int)cfConfig[cGPSRceiverType].v ;

	CONFIG.iPeriodUdpReport = (int)cfConfig[cStatusReportPeriod].v;
	CONFIG.iPeriodHeartBeat = (int)cfConfig[cHeartBeatPeriod].v;
	CONFIG.iTemperatureGain = (int)cfConfig[cTemperatureGain].v;
	CONFIG.iCVType = (int)cfConfig[cCVType].v;
	CONFIG.PosX = (double)cfConfig[cPosX].v;
	CONFIG.PosY = (double)cfConfig[cPosY].v;
	CONFIG.PosZ = (double)cfConfig[cPosZ].v;

	CONFIG.SYSTEM_DELAY = (double)cfConfig[cSYSTEM_DELAY].v;
	CONFIG.FPGACH = (int)cfConfig[cFPGACH].v;
	CONFIG.VMEMODE = (int)cfConfig[cVMEMODE].v;
	CONFIG.NETSEND = (int)cfConfig[cNETSEND].v;
}

char found_revision = NO;
char ReadCONFIG(void)
{
FILE *fp; 
int i;
int argc;
int CFindex;
char buf[64] = {0, };
char* argv[MAX_ARGVS];
double db;
	found_revision = NO;
	//fp = fopen("/gpsclock/config", "r");
	fp = fopen("/app/config", "r");
	
	if( fp == NULL ){
		printf( "Open Error... /app/config... for Read\n");
		LogItString (LOG_CATA1_NORM, LOG_CATA2_FAIL, "File open for read");
		//exit(-1);
		return FAIL;
	}
	
	for(i=0; i<cEnd; i++){
		memset(buf,0,64);
		if(fgets(buf,64,fp) == NULL){
			printf("too small config arguments\n");
			break;
		}
		
		upper_str(buf);
		
		memset(argv,0,MAX_ARGVS);
		argc = parse_args(buf, argv);
		
		if(argc){
			CFindex = 0;
			
	    	while(CFindex < cEnd){
				if( strcasecmp( argv[0], cfConfig[CFindex].name) == 0 ){
					//if( (atof(argv[1]) >= cfConfig[CFindex].min) &&  (atof(argv[1]) <= cfConfig[CFindex].max) ){
					db=atof(argv[1]);
					//printf("\n ==== db: %s ==> %f\n",argv[1],db);
					if( (my_atof(argv[1]) >= cfConfig[CFindex].min) &&  (my_atof(argv[1]) <= cfConfig[CFindex].max) ){

						if(cfConfig[CFindex].type == 'c' || cfConfig[CFindex].type == 'i'){
							cfConfig[CFindex].v = atoi(argv[1]);
						}
						else if(cfConfig[CFindex].type == 'd'){
							//cfConfig[CFindex].v = atof(argv[1]);
							cfConfig[CFindex].v = my_atof(argv[1]);
						}
						else{
							cfConfig[CFindex].v = 0;
						}
							
//						if(CFindex == cHWrevision){
//							found_revision = YES;
//						}
					}
					else{
						printf("invalid config value for %s: %s\n",cfConfig[CFindex].name, argv[1]);
						cfConfig[CFindex].v = cfConfig[CFindex].dft;
						printf("Set to Default:%d\n",(int)cfConfig[CFindex].v);
						found_revision = YES;
						//fclose(fp);
						//return FAIL;
					}	
				}
				CFindex++;
	    	}
		}	
	}

	fclose(fp);
	ApplyCONFIG();
	
//	if(found_revision == NO){
//		printf("HW revision number not found: defaults to 2\n");
//		cfConfig[cHWrevision].v = 2;
//		CONFIG.HWrevision = 2;
//	}

	return OK;
}


void Show_Config(void)
{
	printf("Antdly(Check)     : %ld (%d)\n", CONFIG.ANTDLY, CONFIG.AntCheckFlag);
	printf("Time Mode(Zone)   : %s (%d,%d)\n", 
		(CONFIG.TimeMode ? "UTC":"GPS"), CONFIG.LOCALHOUR,CONFIG.LOCALMINUTE);
	printf("Position          : %s\n", GetPositionString(CONFIG.POSITION));
	printf("OCXO(DAC, Gain)   : %s (%d, %.2E)\n", CONFIG.OCXO_ID, CONFIG.DAC,CONFIG.GAIN);
	
}



void Setting_CONFIG(void)
{
    char result;
	
    PresetCONFIG();
    result = ReadCONFIG();

    if( result != OK ){
		PresetCONFIG();
		CONFIG.WriteEvent = ON;
		TransmitMessage((unsigned char*)"Reading Configuration.. FAIL--> config preset\r\n",PORT_CONTROL);
	}
	else{
		//CONFIG.WriteEvent = OFF;
		if(found_revision == YES){
			CONFIG.WriteEvent = ON;
		}
		else CONFIG.WriteEvent = OFF;
	}

	//CONFIG.AntCheckFlag = YES;

    SYSTEM.POSITION.LASTHELD = CONFIG.POSITION;
    SYSTEM.POSITION.PositionMode = CONFIG.PositionMode;

    SYSTEM.TIME.LOCALHOUR = CONFIG.LOCALHOUR;
    SYSTEM.TIME.LOCALMINUTE = CONFIG.LOCALMINUTE;

    SYSTEM.TIME.TIMEMODE = CONFIG.TimeMode;

   	TransmitMessage((unsigned char*)"Reading Configuration.. OK\r\n",PORT_CONTROL);
   	//MessageOut("Config OK!\r\n");


	if(!(CONFIG.MASK >= 0 && CONFIG.MASK <= 89)){
		CONFIG.MASK = 10;
	}

	MakePWMInterval((float)CONFIG.DAC);

	CONTROL.CurrentDAC = (int)CONFIG.DAC;
	
	//kang ChangeDACValue((int)CONTROL.CurrentDAC);
	ChangeDACValue(fdFPGA, (int)CONTROL.CurrentDAC);

	CONTROL.RealDAC = (float)CONFIG.DAC;
	//SetRbFreq();
	
	sprintf(MESG,"F/W ver:%s, rev:%d, dac:%d, gain:%.2e",
		FirmWare_Version, CONFIG.HWrevision, CONTROL.CurrentDAC, CONFIG.GAIN
	);
	LogItString(LOG_CATA1_NORM, LOG_CATA2_NOTHING, MESG);

}



